<?php

class DatabaseConnection
{
    
    private static $class_instance_;
    private static $instance_ = false;
    private $connection_ = -1;
        
    protected function __construct(){}
        
    public function __destruct(){}
    
    private function __clone(){}
    
    public static function getInstance()
    {
        if(!self::$instance_)
        {
            self::$class_instance_ = new DatabaseConnection();
            self::$instance_ = true;
            return self::$class_instance_;
        }
        return self::$class_instance_;
    }
    
    public function newConnection($connection)
    {
        if($this->connection_ !== -1)
            return $this->connection_;
        try {  
            $this->connection_ = new PDO($connection);  
        }  
        catch(PDOException $e) {  
            echo "Unable to establish a Database Connection.<br />";  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
    
    public function newStatement($statement)
    {
        try {  
            return $this->connection_->prepare($statement); 
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
    
    public function setPlaceholders($arr_params, $statement)
    {
        try { 
            foreach($arr_params as $key => $value) 
                $statement->bindParam($key, $value); 
            var_dump($statement);
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
    
    public function setInteger($arr_params, $statement)
    {
        try { 
            foreach($arr_params as $key => $value) 
                $statement->bindParam($key, $value,PDO::PARAM_INT); 
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
        }  
    }
    
    public function executeStatement($statement)
    {
        try {  
            $statement->execute(); 
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
    
    public function queryStatement($statement)
    {
        try {  
            return $this->connection_->query($statement); 
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
    
    public function setFetchMode($statement, $mode)
    {
        try{
            switch($mode)
            {
                case 'FETCH_OBJ':
                    $statement->setFetchMode(PDO::FETCH_OBJ);
                    break;
                case 'FETCH_ASSOC':
                default:
                    $statement->setFetchMode(PDO::FETCH_ASSOC);
            }
        }  
        catch(PDOException $e) {  
            echo $e->getMessage();
            //file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        }  
    }
}

?>
